﻿ IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'RegistroOTGet')
	BEGIN
		DROP  Procedure RegistroOTGet
	END
GO



CREATE PROCEDURE RegistroOTGet
	(	
		@EmpresaId	AS INT,
		@NumeroOT	AS VARCHAR(30)
	)	
AS
BEGIN	
	SET NOCOUNT ON

	DECLARE @id AS INT

	-- Lista de tablas a ser devueltas a la aplicacion para cargar la hoja de registro	
	SELECT 0 AS TableId, 'Catalog'				AS TableName	UNION
	SELECT 1 AS TableId, 'OrdenTrabajo'			AS TableName	UNION
	SELECT 2 AS TableId, 'OrdenTrabajoDetalle'	AS TableName	UNION
	SELECT 3 AS TableId, 'OrdenTrabajoMaterial'	AS TableName    UNION
	SELECT 4 AS TableId, 'OrdenTrabajoGasto'	AS TableName   -- UNION		

	SELECT @id = id 
	FROM Lit_OrdenTrabajo 
	WHERE EmpresaId = @empresaId
	  AND NumeroOT = @numeroOT

	-- OrdenTrabajo
	SELECT Id, FechaInicial, FechaFinal, NumeroOT, OT.ClienteId, C.Cliente_Nombre AS ClienteNombre, ArticuloId AS ProductoId, Unidades, ManoObraIndirecta, Estado
	FROM Lit_OrdenTrabajo OT
	LEFT JOIN Cliente C ON OT.EmpresaId = C.Emp_id AND OT.ClienteId = C.Cliente_id
	LEFT JOIN Articulo A ON OT.EmpresaId = A.Emp_id AND OT.ArticuloId = A.Art_Id
	WHERE id = @id	  
		
	-- OrdenTrabajoDetalle
	SELECT	Id, OTId, Tipo, Fecha, OperarioId, AyudanteId, MaquinaId, Cantidad, 
			HorasRegular, HorasExtra, HorasDoble, Descripcion
	FROM Lit_OTDetalle
	WHERE OTId = @id
	
	-- OrdenTrabajoMaterial
	SELECT	Id, OTId, Tipo, Fecha, ArticuloId AS MaterialId, Cantidad, OperarioId, Departamento
	FROM Lit_OTMaterial
	WHERE OTId = @id
	
	-- OrdenTrabajoGasto
	SELECT	Id, OTId, GastoId, Monto
	FROM Lit_OTGasto
	WHERE OTId = @id
           	            
	SET NOCOUNT OFF
END

GO